<?php
/**
 * 商圈
 */

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\admin\model\BusinessArea as BusinessAreaModel;
use app\admin\model\Region as RegionModel;
use think\Exception;
use think\facade\Request;

class BusinessArea extends AdminController {

    protected $isArrayResult = ['getlist'];

    public $resubmitLock = ['editpost'];

    /**
     * 权限配置
     * 1查看权限、2管理权限、4导入权限、
     * @var array
     */
    protected $levelConfig = [
        1 => ['getlist','editget'],
        2 => ['editpost', 'deleted', 'editsort'],
        4 => [],
        8 => []
    ];

    /**
     * 区域商圈列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $list = BusinessAreaModel::businesslist($params);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改获取
     * @return false|string
     */
    public function editGet() {
        try {
            $params = $this->checkParams('businessAreaId');
            if ($params === false) return $this->resErr();
            $code = BusinessAreaModel::get_Name($params, $res);
            if (!$code) return $this->resErr();
            $list = BusinessAreaModel::upArea($params);
            return $this->resData('success', ['info' => $res, 'area_list' => $list]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改提交
     * @return false|string
     */
    public function editPost() {
        try {
            $params = $this->checkParams('businessAreaId,name,city_code,lng,lat');
            if ($params === false) return $this->resErr();
            $res = BusinessAreaModel::edit($params);
            if (!$res) {
                return $this->resErr();
            } else {
                $this->resData('编辑成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 商圈删除
     * @return false|string
     */
    public function deleted() {
        try {
            $params = $this->checkParams('businessAreaId');
            if ($params === false) return $this->resErr();
            $res = BusinessAreaModel::deleted($params);
            if (!$res) return $this->resErr();
            else $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改区域排序
     * @return false|string
     */
    public function editSort() {
        try {
            $params = $this->checkParams('ids');
            if ($params === false) return $this->resErr();
            RegionModel::editSort($params);
            $this->resData('修改成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
}
